package com.bw.module_service.ui

import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapView
import com.baidu.mapapi.map.MyLocationData
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.lib_storage.mmkv.AuthMMKV
import com.bw.module_service.R
import com.bw.module_service.databinding.ActivityAddCarBinding
import com.bw.module_service.model.CarModel
import com.bw.module_service.utils.ToastUtils
import com.bw.module_service.viewmodel.ServiceViewModel
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody


/*
这是编辑车辆页面!!!!
 */
@AndroidEntryPoint
@Route(path = Router.Ui.AddCarActivity)
class AddCarActivity : BaseActivity<ServiceViewModel, ActivityAddCarBinding>() {
    var area = ""
    var cartype = ""
    var color = ""
    @Autowired
    lateinit var car: CarModel

    lateinit var mMapView:MapView
    lateinit var mBaiduMap:BaiduMap


    private val mViewModel: ServiceViewModel by viewModels()
    override val mLayoutResId: Int
        get() = R.layout.activity_add_car

    private  fun commitupdateCar(){
        val map = mutableMapOf<String, Any>()
        //  "id": 0,
        //  "licensePlate": "津A55555",
        //  "ownerName": "小红",
        //  "provinceAbbreviation": "津",
        //  "userId": 1
        map["id"] = car.id
        map["licensePlate"] = car.licensePlate
        map["ownerName"] = mBinding.addcarCarowner.text.toString()
        map["provinceAbbreviation"] = car.provinceAbbreviation
        map["userId"] = car.userId
        val json = Gson().toJson(map)
        val mediaType = "application/json".toMediaTypeOrNull()
        val body = json.toRequestBody(mediaType)
        mViewModel.updateCar(car.id,body)
    }
    override fun initData() {
        //添加车辆
        mBinding.duigou.setOnClickListener {
            commitupdateCar()
        }
        mBinding.addCar.setOnClickListener {
            commitupdateCar()
        }

        //返回
        mBinding.back.setOnClickListener {
            finish()
        }


        mMapView=mBinding.bmapView
        mBaiduMap = mMapView.map
        mMapView.map.setMyLocationEnabled(true);


        //定位初始化
        val mLocationClient = LocationClient(this)

        //通过LocationClientOption设置LocationClient相关参数
        val option = LocationClientOption()
        option.isOpenGps = true // 打开gps
        option.setCoorType("bd09ll") // 设置坐标类型
        option.setScanSpan(1000)


        //设置locationClientOption
        mLocationClient.setLocOption(option)


        //注册LocationListener监听器
        val myLocationListener = MyLocationListener()
        mLocationClient.registerLocationListener(myLocationListener)

        //开启地图定位图层
        mLocationClient.start()


    }

    override fun initView() {
        val arr = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, arr)
        mBinding.addcarArea.adapter = adapter
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mBinding.addcarArea.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                area = arr[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }

        val arrcarType = arrayOf("宝马七系", "法拉利拉法", "保时捷911")
        val adapter2 = ArrayAdapter(this, android.R.layout.simple_spinner_item, arrcarType)
        mBinding.addcarCartype.adapter = adapter2
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mBinding.addcarCartype.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                cartype = arrcarType[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }

        val arrcarcolor = arrayOf("红色", "蓝色", "黄色")
        val adapter3 = ArrayAdapter(this, android.R.layout.simple_spinner_item, arrcarcolor)
        mBinding.addcarCarcolor.adapter = adapter3
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mBinding.addcarCarcolor.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                color = arrcarcolor[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }

        mBinding.addcarCarowner.setText("${car.ownerName}")
        mBinding.addcarCarnum.setText("${car.licensePlate}")

    }

    override fun initListener() {
        mViewModel._updateCarLiveData.observe(this) {
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show()
            finish()
        }
    }

    inner class MyLocationListener : BDAbstractLocationListener() {
        override fun onReceiveLocation(location: BDLocation) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return
            }
            val locData = MyLocationData.Builder()
                .accuracy(location.radius) // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(location.direction).latitude(location.latitude)
                .longitude(location.longitude).build()
            mBaiduMap.setMyLocationData(locData)
        }
    }



}